package com.devicemantra.coordinate.dataobjects;

import java.util.Iterator;
import java.util.List;

import javax.jdo.JDOException;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;

import com.devicemantra.coordinate.ds.tests.PMF;
import com.devicemantra.coordinate.exceptions.dataobjects.CDatabaseException;
import com.devicemantra.coordinate.exceptions.operations.CDepartmentDoesNotExistsException;
import com.devicemantra.coordinate.exceptions.operations.CDuplicateException;
import com.devicemantra.coordinate.exceptions.operations.CException;
import com.devicemantra.coordinate.exceptions.operations.CStateAlreadyExistsException;
import com.devicemantra.coordinate.exceptions.operations.CStateDoesNotExistsException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


/** TODO - Most of the *Factory classes are written very badly 
 * put some thought into optimizing for performance */
public class CGroupFactory 
{
		public static boolean checkGroupExists(String pGroupName){
			boolean lDoesExist = true;
			Key lNewGroupKey = KeyFactory.createKey(CGroup.class.getSimpleName(), pGroupName);
			System.out.println("Key is " + KeyFactory.keyToString(lNewGroupKey));
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try {
				pm.getObjectById(CGroup.class,lNewGroupKey);
			}catch(JDOObjectNotFoundException e){
				lDoesExist = false;
			}finally {
			pm.close();
			}
			return lDoesExist;
		}
	
		public static void createNewGroup(String pGroupName, 
										List<String> pLocationIds) throws CException, CDuplicateException {
			boolean lDoesExist = checkGroupExists(pGroupName);
			if(lDoesExist == true)
				throw new CDuplicateException("Group already exists");
			
			CGroup lGroup = new CGroup(pGroupName,pLocationIds);
			lGroup.setGroupKey(KeyFactory.createKey(CGroup.class.getSimpleName(), pGroupName));
			persistGroup(lGroup);
			
		}
		
		public static CGroup getGroup(String pGroupName) throws CDatabaseException {
			Key lGroupKey = KeyFactory.createKey(CGroup.class.getSimpleName(), pGroupName);
			PersistenceManager pm = PMF.get().getPersistenceManager();
			CGroup lGroup = null;
			try {
					lGroup = pm.getObjectById(CGroup.class,lGroupKey);
					lGroup = pm.detachCopy(lGroup);
				}catch(JDOObjectNotFoundException e){
					throw new CDatabaseException("ERROR: Could not retrieve object");
				}finally {
			pm.close();
			}
			return lGroup;
		}
		
		public static void addLocationsToGroup(String pGroupName,List<String> pLocationIds) throws CException
		{
			if(checkGroupExists(pGroupName)) {
				try {
					CGroup lGroup = getGroup(pGroupName);
					lGroup.addLocations(pLocationIds);
					persistGroup(lGroup);
				} catch (CDatabaseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					throw new CException("ERROR: Fetching CGroup from datastore");
				} 			
			}
		}
		
		public static boolean deleteGroup(String pLocationId) 
		{
			boolean lIsTrue = false;
			String lLocationId = pLocationId;
			PersistenceManager pm = PMF.get().getPersistenceManager();
			pm.getFetchPlan().setMaxFetchDepth(3);
		    List<CLocation> lLocationList = null;
		    javax.jdo.Query query = pm.newQuery(CLocation.class);
			query.setFilter("mLocationId == lLocationId");
			query.declareParameters("String lLocationId");
		    lLocationList = (List<CLocation>) query.execute(lLocationId);
		    lLocationList = (List<CLocation>)pm.detachCopyAll(lLocationList);
		    
		    if(lLocationList.size() > 1 )
		    {
		    	//throw new CDuplicateException("ERROR: More than one CLocation found with Id " + pLocationId);
		    }
		    
		   /* Check if size of list is 1 and lLocationList not null */
		   if( (lLocationList != null) && (lLocationList.size() == 1) ) {
			   lIsTrue = true;
			}
		   return lIsTrue;
		}
		
		
		public static void persistGroup(CGroup pGroup) throws CException
		{
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try {
				pm.makePersistent(pGroup);
			}catch(JDOUserException e) {
				throw new CException(e.getMessage());
			}finally {
			pm.close();
			}
		}
		
		
		
}
